package work.data
{
	import flash.net.SharedObject;

	/**
	 * 
	 * @author 张峰
	 * @QQ:37520626
	 * @Email:7677@163.com
	 * @Date:2012-9-25 上午11:25:23
	 * 
	 */
	public class SoData
	{
		private var _name:String;
		private var _so:SharedObject;
		public function SoData(name:String="ApplicationsData")
		{
			_name=name;
		}
		private function getSo():SharedObject
		{
			if(_so==null)
			{
				_so=SharedObject.getLocal(_name);
			}
			return _so;
		}
		/**
		 * 保存字段 
		 * @param label 字段名
		 * @param data 值
		 * 
		 */
		public function saveField(label:String,data:Object):void
		{
			var so:SharedObject=getSo();
			so.data[label]=data;
			if(!so.data.createDate)
			{
				so.data.createDate=new Date();
			}
			so.flush();
		}
		/**
		 * 获得字段 
		 * @param label 字段名
		 * @return 值
		 * 
		 */
		public function getField(label:String):Object
		{
			var so:SharedObject=getSo();
			if(!so.data[label])
			{
				return null;
			}
			return so.data[label];
		}
		
		/**
		 * 保存数据对象 
		 * @param o 数据对象
		 * @param add 是否追加保存，如果为false则会删除先前的字段 
		 * 
		 */
		public function savaData(o:Object,add:Boolean=true):void
		{
			var so:SharedObject=getSo();
			var p:String;
			if(add)
			{
				var d:Object=so.data;
				var date:Date=so.data.createDate;
				for(p in so.data)
				{
					delete so.data[p];
				}
				so.data.createDate=date;
			}
			for(p in o)
			{
				so.data[p]=o[p];
			}
			so.flush();
		}
		public function save():void
		{
			var so:SharedObject=getSo();
			so.flush();
		}
	}
}